package io.github.iogogogo.scheduled.properties;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.Serializable;

/**
 * Created by tao.zeng on 2024/11/19.
 */
@Data
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "xxl.job")
public class XxlJobProperties implements Serializable {

    private boolean enable = false;

    private String accessToken;

    @NestedConfigurationProperty
    private AdminProperties admin;

    @NestedConfigurationProperty
    private ExecutorProperties executor;


    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(admin.getAddresses());

        xxlJobSpringExecutor.setAppname(executor.getAppname());
        xxlJobSpringExecutor.setAddress(executor.getAddress());
        xxlJobSpringExecutor.setIp(executor.getIp());
        xxlJobSpringExecutor.setPort(executor.getPort());
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(executor.getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(executor.getLogRetentionDays());

        return xxlJobSpringExecutor;
    }

    @Data
    public static class AdminProperties implements Serializable {
        /**
         * 用户名
         */
        private String username;

        /**
         * 密码
         */
        private String password;

        /**
         * 调度中心地址
         */
        private String addresses;
    }

    @Data
    public static class ExecutorProperties implements Serializable {

        /**
         * 执行器地址类型
         */
        private Integer addressType = 0;

        /**
         * 对应 AppName
         */
        private String appname;

        /**
         * 对应 页面显示中文名称
         */
        private String title;

        /**
         * 在addressType为1的情况下，手动录入执行器地址列表，多地址逗号分隔
         */
        private String address;

        private String ip;

        private int port;

        private String logPath;

        private int logRetentionDays;
    }
}
